//-----------------------------------------------------------------------
// <copyright file="ConPointDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class ConPointDAO : ConPointDAOBase
	{
		public ConPointDAO()
		{
		}

		public virtual ConPoint GetConPoint(string description)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsGetByDescription");
                
				dbCommandWrapper.AddInParameter("@description", DbType.String, description);
                
				ConPoint conPoint =  null;
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						conPoint = CreateConPointFromReader(reader);
						reader.Close();
					}
				}
				return conPoint;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetConPointException, ex);
			}
		}

		public virtual int GetConPointBySupplier(string supplierNo)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsGetBySupplier");
                
				dbCommandWrapper.AddInParameter("@SupplierNo", DbType.String, supplierNo);
                
				int conPointID = Convert.ToInt32(DataAccessHelper.ExecuteScalar(database, dbCommandWrapper));
				
				return conPointID;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetConPointException, ex);
			}
		}

		public virtual ConPointCollection GetConPointsListForIDS()
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsGetListForIDS");
                			               
				ConPointCollection conPointCollection = new ConPointCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						ConPoint conPoint = CreateConPointFromReader(reader);
						conPointCollection.Add(conPoint);
					}
					reader.Close();
				}
				
				return conPointCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetConPointsException, ex);
			}
		}
	}
}
